#include<vector>

#include"../log.hpp"

const int thread_num=5;
const int log_num=11000;
std::atomic<int> thread_count{0};

void test_log_sync()
{
    long long tid=tid_to_ll(std::this_thread::get_id());
    DBG_LOG("[thread_%d]tid is:%lld\n",thread_count.fetch_add(1),tid);
    for(int i=0;i<log_num;i++)
    {
        INF("[tid:%lld] log sync test: %d\n",tid,i);
    }
}

int main()
{
    set_level(DBG_LEVEL);

    Log::get_instance()->init("./log_sync.txt");

    std::vector<std::thread> threads;

    DBG_LOG("sync log test started!\n");
    DBG_LOG("start log\n");

    //??
    auto start=std::chrono::steady_clock::now();

    for(int i=0;i<thread_num;i++)
    {
        //push_back需要传入创建好的元素
        //emplace_back支持直接将构造函数需要的参数传递，会使用移动构造函数构造对象
        threads.emplace_back(test_log_sync);
    }
    for(int i=0;i<thread_num;i++)
    {
        threads[i].join();
    }

    auto end=std::chrono::steady_clock::now();
    auto duration=std::chrono::duration_cast<std::chrono::milliseconds>(end-start);

    DBG_LOG("end log\n");
    DBG_LOG("totally write %d items into files\n",thread_num*log_num);
    DBG_LOG("cost time: %d ms\n",static_cast<int>(duration.count()));

    return 0;
}
